package com.yunji.web.monitor;

import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.model.domain.SysOperLogDO;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.system.service.ISysOperLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 *
 * @author ruoyi
 */
@Api(tags = "操作日志信息")
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseController {
    @Autowired
    private ISysOperLogService operLogService;

    @ApiOperation("操作日志列表")
    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    public Response<List<SysOperLogDO>> list(SysOperLogDO operLog) {
        List<SysOperLogDO> list = operLogService.selectOperLogList(operLog);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出操作日志")
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLogDO operLog) {
        List<SysOperLogDO> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLogDO> util = new ExcelUtil<>(SysOperLogDO.class);
        util.exportExcel(response, list, "操作日志");
    }

    @ApiOperation("批量删除操作日志")
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public Response<String> remove(@PathVariable Long[] operIds) {
        Assert.isTrue(operLogService.deleteOperLogByIds(operIds) > 0, "删除失败");
        return ResponseHelper.success();
    }

    @ApiOperation("清空操作日志")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public Response<String> clean() {
        operLogService.cleanOperLog();
        return ResponseHelper.success();
    }
}
